package com.xjlsoft.weather.task;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjlsoft.weather.common.util.DataUtil;
import com.xjlsoft.weather.config.ApplicationContextProvider;
import com.xjlsoft.weather.entity.ExceptionLog;
import com.xjlsoft.weather.entity.QueryLog;
import com.xjlsoft.weather.entity.RequestApi;
import com.xjlsoft.weather.entity.WeatherTask;
import com.xjlsoft.weather.enums.RequestApiEnum;
import com.xjlsoft.weather.enums.WeatherTaskEnum;
import com.xjlsoft.weather.exception.WeatherException;
import com.xjlsoft.weather.mapper.ExceptionLogMapper;
import com.xjlsoft.weather.mapper.RequestApiMapper;
import com.xjlsoft.weather.mapper.WeatherTaskMapper;
import com.xjlsoft.weather.service.WeatherTaskJob;
import com.xjlsoft.weather.service.WeatherTaskService;

/**
 * 任务 03
 *
 * @author 码农猿
 */
public class HighDataTaskTimin implements WeatherTaskJob {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(HighDataTaskTimin.class);

    
	private RequestApiMapper requestApiMapper;
    
	private  WeatherTaskService weatherTaskService;
	
	private WeatherTaskMapper  weatherTaskMapper;
	
	private ExceptionLogMapper  exceptionLogMapper;
    
    @Override
    public void run() {

    	
    	logger.info(">>>>>>开始 高空   数据           HighDataTaskTimin   当前线程名称 {}", Thread.currentThread().getName());
    	
    	requestApiMapper=ApplicationContextProvider.getBean(RequestApiMapper.class);
    	exceptionLogMapper=ApplicationContextProvider.getBean(ExceptionLogMapper.class);
    	
        RequestApi requestapi=requestApiMapper.selectById(RequestApiEnum.highDataTask.getValue());
       
        WeatherTask wt=weatherTaskMapper.getByKey(WeatherTaskEnum.HighDataTask.getValue());
        if(requestapi==null){
        	logger.error("未查询到RequestApi数据,request_api_id="+RequestApiEnum.highDataTask.getValue());
        }
        try {

        	//logger.info(">>>>>>>>>>睡眠27秒>>>>>>>");
        	//Thread.currentThread();
			//Thread.sleep(1000*27);
			weatherTaskService=ApplicationContextProvider.getBean(WeatherTaskService.class);
			
			weatherTaskMapper=ApplicationContextProvider.getBean(WeatherTaskMapper.class);
			
			
			QueryLog  ql=weatherTaskService.createHighDataQuerylogTimin(requestapi,wt);
        	
			if(ql!=null){
				weatherTaskService.saveHighDataJson(ql, requestapi,false);
			}
        	
        	
        	
		} catch (WeatherException we) {
			logger.error("创建  高空  数据，请求数据异常",we);
			//保存失败请求的 原因  task_log表
			ExceptionLog ex=new ExceptionLog();
			ex.setExceptionId(IdWorker.getIdStr());
			ex.setCreateTime(DataUtil.getCurrenTimestamp());
			ex.setMessage(we.toString());
			StackTraceElement[] st=we.getStackTrace();
			if(st !=null){
				ex.setCodeStr(st[0].toString());
			}
			exceptionLogMapper.insert(ex);
		}  catch (Exception e) {
			e.printStackTrace();
		}
        logger.info(">>>>>>开始 高空   数据           HighDataTaskTimin   当前线程名称 {}", Thread.currentThread().getName());
    
    }
}